import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import {
  DeviceRealTime,
  DeviceRealTimeRecord,
  DeviceRealTimeRecordNew,
} from "@/api/backend/index";
import qs from "qs";
import _ from "lodash";

export const useDeviceRealTime = defineStore(Names.DeviceRealTime, {
  state: () => {
    return {
      single: <any>{},
      type: <any>[], //问题域的字典
      statusType: <any>[], //晨会问题状态的字典
      tags: <any>{},
      showMoreForm: false, //料号详情表单是否打开
      infoSingle: <any>{},
      beginTime: "",
      endTime: "",
      DefaultFields: <any>[], //默认历史数据字段
      DefaultFieldsString: <any>[], //默认历史数据字段 纯字符串
      endData: <any>[],
      xAxisData: <any>[],
    };
  },
  getters: {},
  actions: {
    //获取设备数据
    getList(filter: any) {
      return new Promise((resolve, reject) => {
        _get(DeviceRealTime + "/list", filter, "json", "")
          .then((res: any) => {
            console.log("获取设备数据", res);
            resolve(res);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取大屏设备数据
    getListScreen(filter: any) {
      return new Promise((resolve, reject) => {
        _get(DeviceRealTime + "/currentDate", filter, "json", "")
          .then((res: any) => {
            console.log("获取大屏设备数据", res);
            resolve(res);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //获取设备历史数据
    getHistoryData(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          DeviceRealTimeRecord +
            "/list?" +
            qs.stringify(filter, { arrayFormat: "repeat" }),
          {},
          "json",
          ""
        )
          .then((res: any) => {
            console.log("获取设备历史数据", res);
            resolve(res);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //遍历所有数据
    getNeddData(data: any[]) {
      let len = data.length,
        temp: any[] = [],
        xAxisData: any[] = [];
      for (let i = 0; i < len; i++) {
        xAxisData.push(data[i].time);
        let newObj: any = {};
        for (let j = 0; j < data[i].data.length; j++) {
          newObj[data[i].data[j].elementAddress] = data[i].data[j].elementValue;
        }
        temp.push(newObj);
      }
      let endArr: any[] = [];
      temp.forEach((item: any, index: any) => {
        console.log("遍历所有数据", item);
        let i = 0;
        for (let attr in item) {
          if (!index) {
            endArr[i] = {
              attr: [],
            };
          }
          (endArr[i].attr as any).push(item[attr]);
          i++;
        }
      });
    },
    //查询设备历史数据图表
    getElectricDataEcharts(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          DeviceRealTimeRecordNew +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then(async (res: any) => {
            console.log("查询机柜历史数据图表", res);
            if (res.rows) {
              this.getNeddData(res.rows);
            }
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
